package com.scitc.pleasurevideo.util;

import android.content.Context;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scitc.pleasurevideo.pojo.PlayMenuTvVo;

import java.util.ArrayList;
import java.util.List;

import static com.scitc.pleasurevideo.data.DataUtil.countOfSubstr;


public class PlaySetMenuUtil {

    private static GetPlayMenuTvList getPlayMenuTvList;

    public interface GetPlayMenuTvList {
        void playmenuTvList(List<PlayMenuTvVo> playMenuTvPojoList);

        void playmenuTv(PlayMenuTvVo playMenuTvPojo);
    }

    public static void getPlayMenuTvList(GetPlayMenuTvList menuTvList) {
        getPlayMenuTvList = menuTvList;
    }


    public static void getPlayMenuTvList(final Context context, final String url) {

        OkGo.<String>get(url)
                .tag(context)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        List<PlayMenuTvVo> playMenuTvPojoList = new ArrayList<>();

                        //先判断有没有
                        int frist_len = response.body().indexOf("copy_sel");
                        if (frist_len < 0) {
                            return;
                        }

                        //找到区间
                        int end_len = frist_len;

                        while (true) {
                            int s = response.body().indexOf("</ul>", end_len);
                            int l = response.body().indexOf("<ul", end_len);
                            end_len = s + 5;

                            if (l > s) {
                                break;
                            }

                        }

                        String long_string = response.body().substring(frist_len, end_len);

                        if (long_string.indexOf("m3u8") > 0) {

                            //先判断有没有
                            frist_len = response.body().indexOf("copy_sel", end_len);
                            if (frist_len < 0) {
                                return;
                            }

                            //找到区间
                            end_len = frist_len;

                            while (true) {

                                int s = response.body().indexOf("</ul>", end_len);
                                int l = response.body().indexOf("<ul", end_len);

                                end_len = s + 5;

                                if (l > s) {
                                    break;
                                }

                            }

                            long_string = response.body().substring(frist_len, end_len);
                        }


                        //判断搜索到几个内容
                        int num = countOfSubstr(long_string, "copy_sel");

                        int temp_end_len = 0;
                        for (int i = 0; i < num; i++) {

                            PlayMenuTvVo menuTvPojo = new PlayMenuTvVo();

                            //找到要截取的字符串
                            int flen = long_string.indexOf("checked", temp_end_len);
                            flen = long_string.indexOf(">", flen);
                            int elen = long_string.indexOf("</li>", flen);

                            String tempString = long_string.substring(flen + 1, elen);
                            String[] splitstr = tempString.split("\\$");

                            if (splitstr[0].indexOf(">") > 0) {

                                String temp = splitstr[0].substring(splitstr[0].indexOf(">") + 1, splitstr[0].length());
                                splitstr[0] = temp;

                            }
                            if (splitstr[1].indexOf("<") > 0) {
                                String temp = splitstr[1].substring(0, splitstr[1].indexOf("<"));
                                splitstr[1] = temp;
                            }

                            menuTvPojo.setName(splitstr[0]);
                            menuTvPojo.setPlayWebUrl(splitstr[1]);

                            playMenuTvPojoList.add(menuTvPojo);

//                        getPlayMenuTvList.playmenuTv(menuTvPojo);
                            temp_end_len = elen;
                        }

                        getPlayMenuTvList.playmenuTvList(playMenuTvPojoList);

                    }
                });


    }

}
